<?php
//获取access_token
function access_token(){	
		$APPID=C('APPID');
		$APPSECRET=C('APPSECRET');
		$token=M('token');
		$require=$token->find();
		$id=$require['id'];
		$access_token=$require['token'];
		$ctime=$require['ctime'];
		$time=time();
		$newtime=$ctime+7000;	
		if(empty($access_token)){
		 
			$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$APPID. "&secret=" .$APPSECRET;
			$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
			//print_r($res);exit('123');
			$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
			$access_token = $result['access_token']; 
			//print_r($access_token);exit('111');
			$data=array('token'=>$access_token,'ctime'=>$time,);
			$re=$token->add($data);	
			if(!$re){
				echo "参数错误1";		
			}
		
		}
		if(!empty($ctime) && $time>=$newtime){				
			$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$APPID. "&secret=" .$APPSECRET;
			$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
			//echo $res;
			$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
			$access_token = $result['access_token'];
			$data=array('token'=>$access_token,'ctime'=>$time,);
			$re=$token->where('id='.$id)->save($data);		
			if(!$re){
				echo "参数错误2";	
				}		
			}
			return $access_token;
	}

//获取用户列表
function user_list(){//exit('###');
	set_time_limit(0);
	$access_token=access_token();
	//print_r($access_token);exit('@@@');
	$next_openid='';
	$url='https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='.$next_openid.'';
	$res = file_get_contents($url); 	
	$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
	//print_r($result);exit;
	$openid_list=$result['data']['openid'];
	//print_r($openid_list);exit;
	foreach ($openid_list as $key=>$value) {
		$openid=$value;
		$where['openid']=$openid;
		$res_select=M('user')->where($where)->find();
		if (empty($res_select)) {
		$info_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		$info_res = file_get_contents($info_url); //获取文件内容或获取网络请求的内容
		$info_result = json_decode($info_res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		//查询结果，如果有则不进行插入
		
		//添加数据
		$data = array('subscribe' =>$info_result['subscribe'] ,
					  'openid' =>$info_result['openid'] ,
					  'nickname' =>$info_result['nickname'] ,
					  'sex' => $info_result['sex'],
					  'language' =>$info_result['language'] ,
					  'city' =>$info_result['city'] ,
					  'province' =>$info_result['province'] ,
					  'country' =>$info_result['country'] ,
					  'headimgurl' =>$info_result['headimgurl'] ,
					  'subscribe_time' =>date('Y-m-d H:i:s',$info_result['subscribe_time']),
					  'remark' =>$info_result['remark'] ,
					  'groupid' =>$info_result['groupid'] ,
					  'tagid_list' =>$info_result['tagid_list'] ,
					 
					  'ctime'=>time(),
						 );
		$res_add=M('user')->data($data)->add();
	 }
	}
}
function createMenu($data,$access_token){
	//	print_r($access_token);print_r($data);exit;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$tmpInfo = curl_exec($ch);
	if (curl_errno($ch)) {
	  return curl_error($ch);
	}
	curl_close($ch);
	
	$new_tmpInfo=json_decode($tmpInfo,true);
	$errcode=$new_tmpInfo['errcode'];
	$errmsg=$new_tmpInfo['errmsg'];
	if ($errcode == 0 && $errmsg=='ok') {
		echo '微信菜单生成成功';
	}else{
		echo '微信菜单生成失败';
	}
	//print_r($errcode);print_r($errmsg);exit;
	return $tmpInfo;
}
	/**
	 * 错误跳转
	 */
	function error($msg,$url){
		header("Content-type:textml;charset=utf-8");
		echo "<script>alert('$msg');window.location.href='$url';</script>";exit;
	}


?>